using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Codes.Models;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.IdentityModel.Tokens;
namespace Codes.Controllers;
public class ScoresController : Controller
{
    private readonly StudentInfoDbContext? Db;
    public ScoresController()
    {
      Db=new StudentInfoDbContext();
    }
    public IActionResult Index(string keyword)
    {
        // var list=Db?.Scores.ToList();
        var scores=Db.Scores.AsQueryable();
        if(!string.IsNullOrEmpty(keyword)){
        scores= scores.Where(x=>x.Name.Contains(keyword)||x.Scores.ToString().Contains(keyword));
        }
        return View(scores.ToList());
    }
     public IActionResult Add()
    {
        
        return View();
    }
    [HttpPost]
     public IActionResult Add(Score score)
    {
       
            Db.Scores.Add(score);
            Db.SaveChanges();
            return RedirectToAction("Index");  
    }
     public IActionResult Edit(int id)
    {
        var list=Db?.Scores.Find(id);
        return View(list);
    }
    [HttpPost]
        public IActionResult Edit(int id,Score score)
    {
        var list=Db?.Scores.Find(id);
        list.Name=score.Name;
        list.Scores=score.Scores;
        Db.SaveChanges();
        return RedirectToAction("Index");
        
    }
     public IActionResult Delete(int id)
    {
        var list=Db?.Scores.Find(id);
        return View(list);
    }
    [HttpPost]
    public IActionResult Delete(int id,Score score)
    {
        var list=Db?.Scores.Find(id);
        Db.Scores.Remove(list);
        Db.SaveChanges();
        return RedirectToAction("Index"); 
      
    }
}